package cn.gztang.util;


import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Base64;
import java.util.Random;
import java.util.UUID;

/**
 * 字符串工具类
 * @author GaoFeng2017
 * @date 2018-06-13 15:17:57
 **/

public class StringUtil {

    private static final Logger logger = LoggerFactory.getLogger(StringUtil.class);

    private static final String randomBaseStr = "1234567890abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";

    private static final Random random = new Random();

    private static final String ZERO = "0";

    private static char[] hexChars = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
    /**
     * 获取指定长度的随机字符串
     * @author GaoFeng
     * @date 2018/6/13 15:31
     * @param len 生成的字符串长度
     * @return java.lang.String
     *
     */

    public static String getRandomStr(int len) {

        StringBuilder sb = new StringBuilder(len);

        random.setSeed(System.nanoTime());

        for (int i = 0;i < len;i++) {
            sb.append(randomBaseStr.charAt(random.nextInt(randomBaseStr.length())));
        }

        return sb.toString();
    }


    /**
     * 将byte字节数组转换为十六进制字符串
     * @author GaoFeng
     * @date 2018/6/13 15:54
     * @param bys 要转换的byte数组
     * @return java.lang.String
     *
     */

    public static String bytesToHexStr(byte[] bys) {

        /*初始化字符串容器,单个byte字节占8位,可转为两个十六进制字符串*/
        StringBuilder sb = new StringBuilder(bys.length * 2);

        for (byte b : bys) {
            /*获取字节的高4位和低4位，并转为十进制*/
            sb.append(hexChars[(b >>> 4) & 0x0f]);
            sb.append(hexChars[b & 0x0f]);
        }

        return sb.toString();
    }

    /**
     *
     * 将字符串数组的元素进行拼接
     * @author GaoFeng
     * @date 2018/6/13 17:13
     * @param arr 要连接的字符串数组
     * @return java.lang.String
     *
     */
    public static String strArrConnect(String[] arr) {

        StringBuilder sb = new StringBuilder(arr.length);

        for (String s : arr) {
            if (s != null) {
                sb.append(s);
            }
        }

        return sb.toString();
    }

    /**
     *
     * @author ZengZhengFang
     *
     * @date 2018/7/25 17:31
     *
     * @param
     *
     * @return java.lang.String
     *
     */
    public static String getUUID() {
        return UUID.randomUUID().toString();
    }

    public static boolean isBlank(String str) {
        return str == null || str.trim().isEmpty();
    }

    /**
     *
     * 字符串转MD5
     *
     * @author ZengZhengFang
     *
     * @date 2018/8/27 18:34
     *
     * @param content
     *
     * @return java.lang.String
     *
     */
    public static String getMD5(String content) {

        byte[] bys = DigestUtils.md5(content);
        return Base64.getEncoder().encodeToString(bys);
    }

    /**
     *
     * 为指定数字填充零
     *
     * @author ZengZhengFang
     *
     * @date 2018/9/6 16:40
     *
     * @param number
     * @param bitCount
     *
     * @return java.lang.String
     *
     */
    private static String fillNumber(String number, int bitCount) {

        int len = bitCount - number.length();
        StringBuilder sb = new StringBuilder(len + 1);

        for (int i = 0; i < len;i++) {
            sb.append(ZERO);
        }

        return sb.append(number).toString();
    }

    public static String fillLong(Long number, int bitCount) {
        return fillNumber(number.toString(), bitCount);
    }
}
